// 变量声明
var Jxc_typename     = '',//弹窗品种名称
    idMyurl          = GetQueryString("typename"),  //持仓品种
    myurl            = '',
    timer1           = 0,
    labels           = ['FXXAUUSD','FXEURUSD','FXGBPUSD','FXUSDJPY','FXUSDCHF','FXUSDCAD' ,'FXAUDUSD','FXEURGBP','FXEURJPY','FXEURAUD','FXEURCHF','FXEURCAD','FXGBPCHF','FXGBPJPY' ,'FXCADJPY','FXAUDCAD','FXCHFJPY','FXAUDJPY'];  //共有的品种
// 数据调用
$(function(){
    //判断页面有没有这个品种，没有返回一个页面，反之进入页面
	if(myurl !=null && myurl.toString().length>1){
		if($.inArray(myurl,labels>=1)){
			// console.log('品种存在');
		}else{
			// console.log('品种不存在')
		}
	};
    charts_Ajax();
    if((starttime*1000) > nowtime){
        clearInterval(timer1);
    }else if((endtime*1000) < nowtime){
        clearInterval(timer1);
    }else{
        clearInterval(timer1);
        timer1 = setInterval(charts_Ajax,3000);
    }
})
// 判断地址栏
function GetQueryString(name){
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}
//判断用户有没有登录
function localStorageJxc(){
    if(issession == ''){
	    $('#header').hide();
    }else{
        $('#header').show();
    }
}
var jnum = 200;
var str_charts = '&stime='+getdate(10)+'&etime='+getdate(0)+'&num='+jnum+'&return_t=3&qt_type=30';//默认数据调用传值
// echarts图表数据调用
function check_time(times,obj,text){
    clearInterval(timer1);
	$(obj).siblings().each(function(){
		if($(this).find('span').html() != undefined){
			$(this).html($(this).find('span').html());
		}
	});
    $(obj).html('<span class="font">'+text+'</span>');
    if(times == 24){
        str_charts = "&return_t=0&num=50";
    }else{

        if(times == 30){
            jnum = 200;
        }else if(times == 60){
            jnum = 350;
        }else{
            jnum = 1000;
        }
        str_charts = "&stime="+getdate(10)+"&etime="+getdate(0)+"&num="+jnum+"&return_t=3&qt_type="+times;
    }
    timer1 = setInterval(charts_Ajax,3000);
}
//获取某个日期
function getdate(num){
    var now = new Date();
    var date = new Date(now.getTime() - num * 24 * 3600 * 1000);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return year + '-' + month + '-' + day;
}
// echarts图表ajax数据调用
function charts_Ajax(){
    var url    = "http://db2015.wstock.cn/wsDB_API2/kline.php?t="+ (new Date()).getTime()+"&r_type=2"+str_charts+"&symbol="+idMyurl+"&query=Date,Symbol,SP1,BP1,Name,LastClose,Open,High,Low&fq=2&u=liyunhe1228&p=liyunhe1228";
    $.ajax({
        type : "get",
        async : false, //同步请求
        url : url,
        data : '',
        timeout:1000,
        success:function(d){
            var rawData  = [];
            for(var i =0 ;i < d.length;i++){
                var rawData1 = [];
                rawData1.push(d[i].Date,d[i].Open,d[i].Close,d[i].Low,d[i].High);
                rawData.push(rawData1);
            }
            echarts_J(rawData.reverse());
        }
    });
}
// 图表区域
function echarts_J(rawData){
	var myChartPie_a = echarts.init(document.getElementById('echastsA'))
	// var rawData = [['2015/4/28','4527.63','4476.21','-51.18','-1.13%','4432.9','4572.39','767676416','106117216','-'],['2015/4/27','4441.93','4527.4','133.71','3.04%','4441.93','4529.73','671088512','97524208','-'],['2015/4/24','4355.95','4393.69','-20.82','-0.47%','4318.12','4416.38','628555008','91687296','-'],['2015/4/23','4414.48','4414.51','16.01','0.36%','4358.84','4444.41','667344640','96302488','-']].reverse();
	function calculateMA(dayCount, data) {
	    var result = [];
	    for (var i = 0, len = data.length; i < len; i++) {
	        if (i < dayCount) {
	            result.push('-');
	            continue;
	        }
	        var sum = 0;
	        for (var j = 0; j < dayCount; j++) {
	            sum += data[i - j][1];
	        }
	        result.push(sum / dayCount);
	    }
	    return result;
	}
	var dates = rawData.map(function (item) {
	    return item[0];
	});
	var data = rawData.map(function (item) {
	    return [+item[1], +item[2], +item[3], +item[4]];
	});
	var option = {
	    backgroundColor: '#21202D',
	    legend: {
	        // data: ['日K', 'MA5', 'MA10', 'MA20', 'MA30'],
	        inactiveColor: '#777',
	        textStyle: {
	            color: '#fff'
	        }
	    },
	    tooltip: {
	        trigger: 'axis',
	        axisPointer: {
	            // animation: false,
	            type: 'cross',
                trigger: 'axis',
                axisPointer: {
                    type: 'line'
                }
	        }
	    },
	    xAxis: {
	        type: 'category',
	        data: dates,
	        axisLine: { lineStyle: { color: '#8392A5' } }
	    },
	    yAxis: {
	        scale: true,
	        axisLine: { lineStyle: { color: '#8392A5' } },
	        splitLine: { show: false },
            axisLabel:{
                 //X轴刻度配置
                 interval:80 //0：表示全部显示不间隔；auto:表示自动根据刻度个数和宽度自动设置间隔个数
            }
	    },
          grid: [           {
                left: '15%',
                bottom: '28%',
                height: '60%'
            },{
                left: '3%',
                right: '1%',
                top: '82%',
                height: '90%'
            }],
	    dataZoom: [{
	        textStyle: {
	            color: '#8392A5'
	        },
	        handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
	        handleSize: '80%',
	        dataBackground: {
	            areaStyle: {
	                color: '#8392A5'
	            },
	            lineStyle: {
	                opacity: 0.8,
	                color: '#8392A5'
	            }
	        },
	        handleStyle: {
	            color: '#fff',
	            shadowBlur: 3,
	            shadowColor: 'rgba(0, 0, 0, 0.6)',
	            shadowOffsetX: 2,
	            shadowOffsetY: 2
	        }
	    }, {
	        type: 'inside'
	    }],
	    animation: false,
	    series: [
	        {
	            type: 'candlestick',
	            // name: '日K',
	            data: data,
	            itemStyle: {
	                normal: {
	                    color: '#FD1050',
	                    color0: '#0CF49B',
	                    borderColor: '#FD1050',
	                    borderColor0: '#0CF49B'
	                }
	            }
	        }
	    ]
	};
	myChartPie_a.setOption(option);
	window.onresize = function () {
		myChartPie_a.resize(); //使第一个图表适应
	}
}